Reading and Writing CSV Files Using the csv Module in Python
Course: Python Training
Topic: Reading and Writing CSV Files Using the csv Module
Course Link: Python Training Course – JustAcademy
Demo Link: Register for a Course Demo
Website: JustAcademy
1. Introduction to CSV Files in Python
CSV stands for Comma-Separated Values. It is a widely used text-based format for storing structured data in rows and columns. Each row typically represents a record, while each column represents a field or attribute.
CSV files are commonly used to exchange information between spreadsheets, databases, reporting systems, analytics tools, and Python applications. For example, a student management application may use a CSV file to store student names, email addresses, courses, and marks.
Python provides a built-in module called csv for reading and writing CSV data. The module handles important details such as delimiters, quoted fields, embedded commas, and line breaks inside quoted values.
Example of CSV Data
Consider a file named students.csv containing the following information:
StudentID,Name,Course,Marks
101,Rahul,Python,85
102,Priya,Data Analytics,92
103,Amit,Web Development,78
In this example, the first row contains the column headings, and the remaining rows contain student records.
Why Use CSV Files?
- Simple structure: CSV files are easy to create, read, and inspect.
- Data exchange: They can transfer information between many applications.
- Spreadsheet compatibility: Many spreadsheet applications can import and export CSV files.
- Lightweight storage: CSV stores plain text rather than a complex binary structure.
- Data analysis: CSV files are often used to load datasets into Python for cleaning, transformation, and analysis.
- Reporting: Applications can generate CSV reports containing calculated or processed results.
2. Understanding the csv Module
The csv module is included with Python, so it normally does not need to be installed separately. It provides functions and classes for processing tabular text data while respecting CSV quoting rules.
Importing the Module
import csv
After importing the module, you can use methods such as csv.reader(), csv.writer(), csv.DictReader(), and csv.DictWriter().
| Function or Class |
Purpose |
csv.reader() |
Reads records as lists of strings. |
csv.writer() |
Writes records from sequences such as lists or tuples. |
csv.DictReader() |
Reads records as dictionaries, usually using the first row as field names. |
csv.DictWriter() |
Writes dictionary-based records using specified field names. |
writerow() |
Writes one record. |
writerows() |
Writes multiple records. |
The CSV module is preferable to splitting each line manually with split(","), because valid CSV fields may contain commas, quotes, and embedded line breaks.
3. Creating a CSV File Manually
A CSV file can be created with a text editor, a spreadsheet application, or a Python program. A simple CSV file uses commas to separate fields and newline characters to separate records.
Example: employees.csv
EmployeeID,Name,Department,Salary
201,Neha,HR,45000
202,Arjun,IT,65000
203,Meera,Finance,58000
Save the file as employees.csv. The file contains four columns: employee ID, name, department, and salary.
Important CSV Concepts
- Record: A row representing one item or entity.
- Field: An individual value within a record.
- Header: The row containing column names, if the file uses one.
- Delimiter: The character separating fields, commonly a comma.
- Quoted field: A field enclosed in quotation marks when needed to represent special characters correctly.
- Line ending: The character or characters separating records in the file.
4. Reading CSV Files with csv.reader()
The csv.reader() function reads CSV data and produces an iterator over records. Each record is returned as a list of strings.
Basic Syntax
csv.reader(file_object, dialect='excel', **fmtparams)
The file object should generally be opened in text mode with newline="". For files you expect to contain UTF-8 text, specify encoding="utf-8".
Example: Reading All Records
import csv
with open("students.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.reader(file)
for row in reader:
print(row)
Possible output:
['StudentID', 'Name', 'Course', 'Marks']
['101', 'Rahul', 'Python', '85']
['102', 'Priya', 'Data Analytics', '92']
['103', 'Amit', 'Web Development', '78']
Each row is a list. The values are strings by default, including values that appear numeric in the CSV file.
Accessing Individual Fields
import csv
with open("students.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.reader(file)
for row in reader:
if row:
print("ID:", row[0])
print("Name:", row[1])
print("Course:", row[2])
print("Marks:", row[3])
print("-" * 25)
List indexing starts at zero. Therefore, row[0] accesses the first field and row[1] accesses the second field. Make sure the row contains the expected number of fields before accessing an index.
5. Skipping the Header Row
Many CSV files contain column names in their first row. If you only want to process the records, you can skip the header using next().
Example
import csv
with open("students.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.reader(file)
header = next(reader, None)
print("Columns:", header)
for row in reader:
print("Student:", row[1], "| Marks:", row[3])
The expression next(reader, None) returns the first record or None if the iterator is empty. This avoids an error when the file contains no records.
Why Skip the Header?
- Prevents column headings from being treated as ordinary data.
- Makes it easier to convert field values to appropriate data types.
- Allows calculations such as averages to operate only on data rows.
- Helps separate metadata from actual records.
6. Reading CSV Files with csv.DictReader()
The csv.DictReader() class reads each record as a dictionary. The keys usually come from the first row, allowing you to access values by column name rather than by numeric index.
Example
import csv
with open("students.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
print("Name:", row["Name"])
print("Course:", row["Course"])
print("Marks:", row["Marks"])
For the row containing Rahul's information, the dictionary resembles:
{
'StudentID': '101',
'Name': 'Rahul',
'Course': 'Python',
'Marks': '85'
}
Advantages of DictReader
- Improves code readability by using meaningful column names.
- Reduces dependence on column positions.
- Works well when processing reports and business datasets.
- Supports explicit field names when a file does not contain a header.
Using Explicit Field Names
import csv
fieldnames = ["StudentID", "Name", "Course", "Marks"]
with open("students_without_header.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file, fieldnames=fieldnames)
for row in reader:
print(row["Name"], row["Course"])
When fieldnames is supplied, the first row is treated as data rather than automatically being consumed as a header. Use this approach only when the file does not contain a header or when you intentionally want to treat its first row as a record.
7. Writing CSV Files with csv.writer()
The csv.writer() function creates a writer object that converts sequences of values into correctly formatted CSV records.
Basic Syntax
csv.writer(file_object, dialect='excel', **fmtparams)
Example: Writing a Header and Records
import csv
with open("products.csv", "w", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerow(["ProductID", "ProductName", "Price"])
writer.writerow([1, "Keyboard", 1200])
writer.writerow([2, "Mouse", 650])
writer.writerow([3, "Monitor", 8500])
print("CSV file created successfully.")
The resulting file contains a header followed by three product records. The CSV writer handles formatting values and quoting fields when required.
Understanding writerow()
The writerow() method writes a single record. The argument is generally a list or tuple containing the field values for that row.
writer.writerow(["101", "Rahul", "Python", 85])
Writing Multiple Records with writerows()
import csv
students = [
["StudentID", "Name", "Course", "Marks"],
[101, "Rahul", "Python", 85],
[102, "Priya", "Data Analytics", 92],
[103, "Amit", "Web Development", 78]
]
with open("students_output.csv", "w", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerows(students)
The writerows() method accepts an iterable of records. It is convenient when the data is already available as a list of lists or another iterable.
8. Writing CSV Files with csv.DictWriter()
The csv.DictWriter() class writes dictionary-based records to a CSV file. You specify the column names using the fieldnames parameter.
Example
import csv
fieldnames = ["EmployeeID", "Name", "Department", "Salary"]
employees = [
{
"EmployeeID": 201,
"Name": "Neha",
"Department": "HR",
"Salary": 45000
},
{
"EmployeeID": 202,
"Name": "Arjun",
"Department": "IT",
"Salary": 65000
},
{
"EmployeeID": 203,
"Name": "Meera",
"Department": "Finance",
"Salary": 58000
}
]
with open("employees_output.csv", "w", newline="", encoding="utf-8") as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(employees)
print("Employee CSV file saved.")
Important DictWriter Methods
| Method |
Purpose |
writeheader() |
Writes the configured field names as the first row. |
writerow() |
Writes one dictionary as a record. |
writerows() |
Writes multiple dictionaries as records. |
Handling Extra Dictionary Keys
By default, DictWriter raises a ValueError if a dictionary contains keys that are not included in fieldnames. You can control this with the extrasaction parameter.
import csv
fieldnames = ["Name", "Course"]
with open("course_records.csv", "w", newline="", encoding="utf-8") as file:
writer = csv.DictWriter(
file,
fieldnames=fieldnames,
extrasaction="ignore"
)
writer.writeheader()
writer.writerow({
"Name": "Priya",
"Course": "Python",
"City": "Mumbai"
})
In this example, the extra City key is ignored. The default behavior is often preferable when unexpected fields should be detected rather than silently discarded.
9. Handling Commas, Quotes, and Special Characters
CSV data can contain commas, quotation marks, and even line breaks inside field values. These cases are one of the main reasons to use Python's csv module instead of manually splitting or joining strings.
Example: A Value Containing a Comma
import csv
with open("contacts.csv", "w", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerow(["Name", "Address"])
writer.writerow(["Rahul", "Bhayandar, Maharashtra"])
The CSV writer automatically quotes the address field when necessary. A typical representation is:
Name,Address
Rahul,"Bhayandar, Maharashtra"
Example: A Value Containing Double Quotes
import csv
with open("quotes.csv", "w", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerow(["Name", "Comment"])
writer.writerow(["Priya", 'She said "Python is useful."'])
The writer escapes embedded quote characters according to the selected CSV dialect and quoting rules.
Reading Quoted Fields Correctly
import csv
with open("contacts.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.reader(file)
for row in reader:
print(row)
Python interprets quoted fields as part of a single field, even when the field contains a comma. Avoid using line.split(",") for general CSV parsing because it does not correctly handle these cases.
10. Working with Different Delimiters
Although commas are common, some files use tabs, semicolons, or other characters to separate fields. The delimiter parameter allows you to specify the separator.
Reading a Semicolon-Separated File
Example file, employees_semicolon.csv:
EmployeeID;Name;Department
201;Neha;HR
202;Arjun;IT
import csv
with open("employees_semicolon.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.reader(file, delimiter=";")
for row in reader:
print(row)
Writing a Tab-Separated File
import csv
with open("students.tsv", "w", newline="", encoding="utf-8") as file:
writer = csv.writer(file, delimiter="\t")
writer.writerow(["ID", "Name", "Course"])
writer.writerow([101, "Rahul", "Python"])
writer.writerow([102, "Priya", "Data Analytics"])
Although the file extension is .tsv, the Python CSV module can still process it by using a tab as the delimiter.
11. Understanding CSV Dialects and Quoting
A CSV dialect is a collection of formatting rules, such as the delimiter, quote character, line terminator, and quoting behavior. The csv module includes predefined dialects and also allows custom formatting parameters.
| Parameter |
Meaning |
delimiter |
The character separating fields, such as a comma or semicolon. |
quotechar |
The character used to enclose quoted fields, usually a double quote. |
quoting |
Controls when fields are quoted. |
doublequote |
Controls whether a quote character inside a quoted field is represented by doubling it. |
escapechar |
An optional character used to escape special characters in supported configurations. |
lineterminator |
The sequence used to terminate output records. |
skipinitialspace |
Controls whether spaces immediately following a delimiter are skipped when reading. |
Example: Controlling Quoting
import csv
with open("report.csv", "w", newline="", encoding="utf-8") as file:
writer = csv.writer(
file,
quoting=csv.QUOTE_MINIMAL
)
writer.writerow(["Name", "City"])
writer.writerow(["Rahul", "Mumbai"])
writer.writerow(["Priya", "Pune, Maharashtra"])
csv.QUOTE_MINIMAL quotes fields only when necessary, such as when they contain the delimiter, quote character, or a line break.
Common Quoting Options
csv.QUOTE_MINIMAL: Quote only fields that need quoting.
csv.QUOTE_ALL: Quote every field written.
csv.QUOTE_NONNUMERIC: When writing, quote non-numeric fields; when reading, unquoted fields are converted to floating-point numbers.
csv.QUOTE_NONE: Do not apply normal quoting; an escape character may be needed when special characters occur.
Some additional quoting constants are available in newer Python versions. When writing code intended for multiple Python versions, check the supported constants for the target version.
12. Converting CSV Values into Appropriate Data Types
CSV files store textual representations of values. When reading with csv.reader() or csv.DictReader(), values normally arrive as strings. Applications often need to convert them into integers, floating-point numbers, dates, or other suitable types.
Example: Converting Student Marks
import csv
with open("students.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
student_name = row["Name"]
marks = int(row["Marks"])
print(f"{student_name} scored {marks} marks.")
Here, the Marks field is converted from a string into an integer before it is used in calculations.
Calculating Average Marks
import csv
marks_list = []
with open("students.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
marks = int(row["Marks"])
marks_list.append(marks)
if marks_list:
average = sum(marks_list) / len(marks_list)
print("Average marks:", average)
else:
print("No student records found.")
Handling Invalid Numeric Values
import csv
with open("students.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
try:
marks = int(row["Marks"])
except (ValueError, TypeError):
print("Invalid marks value:", row.get("Marks"))
continue
print(row["Name"], marks)
In real datasets, values may be missing or malformed. Validate them before conversion and decide whether invalid rows should be skipped, reported, or corrected.
13. Filtering and Searching CSV Records
After reading a CSV file, you can filter records based on conditions. This is useful for extracting selected employees, finding products within a price range, or identifying students who meet a score requirement.
Example: Finding Students with Marks Above 80
import csv
with open("students.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
try:
marks = int(row["Marks"])
except (ValueError, TypeError):
continue
if marks > 80:
print(row["Name"], marks)
Example: Filtering Employees by Department
import csv
target_department = "IT"
with open("employees.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for employee in reader:
if employee.get("Department", "").casefold() == target_department.casefold():
print(employee["Name"], employee["Salary"])
The casefold() method performs a case-insensitive comparison for text. The salary remains a string in this example; convert it to a numeric type before performing arithmetic.
14. Updating Data in a CSV File
The CSV module does not provide a dedicated operation for changing a field in place. A common strategy is to read the existing records, modify the required values, and write the updated records to a new file or replace the original file after successful processing.
Example: Updating a Student's Marks
import csv
from pathlib import Path
source_path = Path("students.csv")
temporary_path = Path("students_updated.tmp")
with source_path.open("r", newline="", encoding="utf-8") as source:
reader = csv.DictReader(source)
fieldnames = reader.fieldnames
if fieldnames is None:
raise ValueError("The CSV file has no header.")
rows = list(reader)
for row in rows:
if row.get("StudentID") == "101":
row["Marks"] = "90"
with temporary_path.open("w", newline="", encoding="utf-8") as destination:
writer = csv.DictWriter(destination, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)
print("Updated records saved to", temporary_path)
This example writes the modified records to a separate file rather than replacing the source immediately. After verifying the output, the application can perform a carefully managed replacement if required.
Important Considerations
- Back up important source files before replacing them.
- Validate the new file before treating the update as complete.
- Consider how duplicate IDs and missing columns should be handled.
- For large files, avoid loading all rows into memory when a streaming transformation is possible.
- For concurrent updates or complex records, a database is often more suitable than a CSV file.
15. Handling Empty Files and Missing Values
CSV files may contain no records, missing fields, or empty strings. Your code should define how such cases are handled rather than assuming every row is complete.
Checking for an Empty File
import csv
with open("students.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
if reader.fieldnames is None:
print("The file is empty or has no header.")
else:
for row in reader:
print(row)
An empty file has no header. A file containing only a header has field names but no data rows.
Handling Missing Fields
import csv
with open("students.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
name = row.get("Name") or "Unknown"
course = row.get("Course") or "Not provided"
print(name, course)
The get() method returns None when a key is absent. The expression or also substitutes the fallback value when the field is an empty string.
Rows with Unexpected Field Counts
When using DictReader, extra values in a row can be stored under the key specified by restkey, and missing values can receive the value specified by restval. These options help when working with inconsistent files.
import csv
with open("students.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(
file,
restkey="ExtraFields",
restval="Missing"
)
for row in reader:
print(row)
16. Using newline="" Correctly
When opening CSV files for reading or writing, Python's documentation recommends using newline="". This allows the csv module to handle newline processing correctly.
Recommended Pattern
import csv
with open("data.csv", "w", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerow(["ID", "Name"])
writer.writerow([1, "Rahul"])
Why Is It Important?
- Helps avoid unintended extra blank lines on some platforms when writing CSV records.
- Allows the CSV parser to handle line endings and quoted fields containing newlines correctly.
- Makes CSV file behavior more consistent across operating systems.
17. Reading and Writing CSV Files with pathlib
The pathlib module provides a convenient way to represent file paths. You can combine pathlib.Path with the CSV module for readable file-handling code.
Example
import csv
from pathlib import Path
csv_path = Path("data") / "products.csv"
csv_path.parent.mkdir(parents=True, exist_ok=True)
with csv_path.open("w", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerow(["ProductID", "Name", "Price"])
writer.writerow([1, "Keyboard", 1200])
writer.writerow([2, "Mouse", 650])
with csv_path.open("r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for product in reader:
print(product["Name"], product["Price"])
This example creates the parent directory if necessary, writes the CSV file, and then reads its records using column names.
18. Handling CSV Errors and Troubleshooting
CSV files may be malformed or inconsistent. Robust applications should handle expected file-system exceptions and understand the errors raised by the CSV parser.
| Problem |
Possible Cause |
Solution |
FileNotFoundError |
The path is incorrect or the file is missing. |
Verify the file path and working directory. |
PermissionError |
The program lacks permission to access the file. |
Check permissions and whether the file is being restricted by another process. |
| Extra blank rows |
The file was opened for writing without newline="" on some platforms. |
Use newline="" when opening CSV files. |
| Columns appear merged |
The wrong delimiter was used. |
Set the correct delimiter. |
| Incorrect column names |
The header is missing, misspelled, or formatted differently. |
Inspect the first row and configure the expected field names. |
ValueError while writing dictionaries |
A dictionary has unexpected keys. |
Align the keys with fieldnames or choose an appropriate extrasaction. |
UnicodeDecodeError |
The file is not encoded as UTF-8 or contains invalid byte sequences. |
Identify the actual encoding and use the correct one. |
| Incorrect numeric calculations |
CSV values were not converted from strings. |
Validate and convert numeric fields explicitly. |
Example: Handling Common File Errors
import csv
try:
with open("students.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
except FileNotFoundError:
print("The CSV file could not be found.")
except PermissionError:
print("Permission denied while accessing the CSV file.")
except UnicodeDecodeError:
print("The file could not be decoded using UTF-8.")
except csv.Error as error:
print(f"CSV parsing error: {error}")
except OSError as error:
print(f"File operation failed: {error}")
Specific handlers make error messages clearer. The exact exceptions raised depend on the operation and the nature of the malformed input. For strict validation, consider the reader's strict=True option and validate the number and meaning of fields yourself.
19. CSV Security and Data Integrity
CSV files are simple to exchange, but they can contain untrusted input. Applications should consider how the data will be used, especially when files are opened in spreadsheet programs or processed by other systems.
CSV Formula Injection
Spreadsheet applications may interpret cells beginning with characters such as =, +, -, or @ as formulas. If a CSV file contains untrusted user-provided values, opening it in a spreadsheet may create a formula-injection risk.
Example of Untrusted Data
user_comment = input("Enter a comment: ")
with open("comments.csv", "w", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerow(["Comment"])
writer.writerow([user_comment])
The CSV writer correctly formats CSV fields, but it does not guarantee that spreadsheet applications will treat every value as harmless text. If the file will be opened in spreadsheet software, evaluate a spreadsheet-specific sanitization policy for untrusted values. Prefixing suspicious values with an apostrophe is one common mitigation, but behavior varies by application and data format.
Data Integrity Recommendations
- Validate required columns and data types before processing records.
- Do not assume that CSV quoting makes arbitrary content safe for every application.
- Keep backups before replacing important files.
- Restrict access to sensitive exported data.
- Check for duplicate identifiers and incomplete records.
- Document the delimiter, encoding, column order, and expected data types for shared CSV formats.
20. Complete Mini-Project: Student Record Manager
This project demonstrates how to create, read, filter, and append student records using the CSV module. It combines several concepts covered in this lesson.
Project Requirements
- Create a CSV file with student ID, name, course, and marks.
- Write a header when the file is first created.
- Add new student records without deleting existing data.
- Read and display saved student records.
- Find students whose marks meet a specified threshold.
- Handle missing files and invalid numeric input.
Complete Python Program
import csv
from pathlib import Path
FILE_PATH = Path("student_records.csv")
FIELDNAMES = ["StudentID", "Name", "Course", "Marks"]
def initialize_file():
if not FILE_PATH.exists() or FILE_PATH.stat().st_size == 0:
with FILE_PATH.open("w", newline="", encoding="utf-8") as file:
writer = csv.DictWriter(file, fieldnames=FIELDNAMES)
writer.writeheader()
def add_student(student_id, name, course, marks):
initialize_file()
record = {
"StudentID": student_id,
"Name": name,
"Course": course,
"Marks": marks
}
with FILE_PATH.open("a", newline="", encoding="utf-8") as file:
writer = csv.DictWriter(file, fieldnames=FIELDNAMES)
writer.writerow(record)
print("Student record saved.")
def display_students():
try:
with FILE_PATH.open("r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
print(
f"ID: {row['StudentID']}, "
f"Name: {row['Name']}, "
f"Course: {row['Course']}, "
f"Marks: {row['Marks']}"
)
except FileNotFoundError:
print("No student records found.")
def find_high_scorers(minimum_marks):
try:
with FILE_PATH.open("r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
try:
marks = int(row["Marks"])
except (ValueError, TypeError):
print("Skipping invalid record:", row)
continue
if marks >= minimum_marks:
print(row["Name"], "-", marks)
except FileNotFoundError:
print("No student records found.")
add_student("101", "Rahul", "Python", 85)
add_student("102", "Priya", "Data Analytics", 92)
print("\nAll student records:")
display_students()
print("\nStudents scoring at least 90:")
find_high_scorers(90)
How the Project Works
- Constants: The file path and field names are defined once for consistency.
- Initialization: The program creates a header if the file is missing or empty.
- Record creation:
add_student() constructs a dictionary containing the student information.
- Appending: The dictionary is written as a new CSV record.
- Displaying records:
display_students() reads the CSV using DictReader.
- Filtering:
find_high_scorers() converts marks to integers and filters records based on the threshold.
- Error handling: The reading functions handle missing files and invalid marks.
This is a learning project rather than a production-grade record system. A larger application should also validate input, detect duplicate student IDs, validate headers, and consider how to avoid race conditions when multiple processes write to the same file.
21. Practice Exercises
Beginner Exercises
- Create a CSV file named
books.csv containing book ID, title, author, and price.
- Read a CSV file using
csv.reader() and display every row.
- Skip the header row and display only the data records.
- Write five programming languages to a CSV file.
- Use
csv.DictReader() to display records by column name.
Intermediate Exercises
- Create an employee CSV file using
csv.DictWriter().
- Find all products whose prices are greater than a user-provided amount.
- Calculate the average marks of students stored in a CSV file.
- Read a semicolon-separated file by specifying
delimiter=";".
- Handle missing files and invalid numeric values using exception handling.
- Write a program that appends new records to an existing CSV file.
Advanced Exercises
- Build a CSV-based inventory manager that adds and lists products.
- Read employee records, filter them by department, and write the filtered records to a new CSV file.
- Update a selected record by reading the source file and writing validated output to a temporary file.
- Detect missing fields and invalid numeric values in a CSV dataset.
- Create a report that counts records by category and writes the summary to a second CSV file.
22. Interview Questions and Answers
Q1. What is a CSV file?
Answer: A CSV file is a text-based format for storing tabular information as records and fields, commonly separated by commas.
Q2. What is the purpose of Python's csv module?
Answer: It provides tools for parsing and generating CSV data, including support for quoted fields, delimiters, and embedded line breaks.
Q3. What is the difference between reader() and DictReader()?
Answer: reader() returns each record as a list, while DictReader() returns records as dictionaries using field names.
Q4. What is the difference between writer() and DictWriter()?
Answer: writer() writes records from sequences such as lists or tuples. DictWriter() writes dictionary-based records according to specified field names.
Q5. Why is newline="" recommended when opening CSV files?
Answer: It lets the CSV module handle newline processing correctly and helps prevent extra blank lines when writing on some platforms.
Q6. What does writerow() do?
Answer: It writes one record to a CSV file.
Q7. What does writerows() do?
Answer: It writes multiple records from an iterable of sequences or dictionaries, depending on the writer being used.
Q8. How can you skip a CSV header?
Answer: With csv.reader(), call next(reader, None) before processing the remaining records. DictReader() normally uses the first row as field names automatically.
Q9. Why should you avoid splitting CSV lines manually with split(",")?
Answer: Manual splitting fails when a field contains a comma inside quotes, embedded quotation marks, or a newline. The CSV module handles these formatting rules correctly.
Q10. Are numeric CSV values automatically converted to integers?
Answer: Not by default when using ordinary reader() or DictReader(). Values are normally returned as strings and should be converted explicitly when required.
Q11. How do you write a header using DictWriter?
Answer: Define fieldnames when creating the writer and call writeheader().
Q12. How do you read a CSV file with a semicolon delimiter?
Answer: Pass delimiter=";" to csv.reader() or csv.DictReader().
Q13. Can CSV files store nested Python objects directly?
Answer: CSV does not define a native representation for nested objects. Applications must choose how to serialize such values, or use a format such as JSON when nested data is required.
Q14. How can you update an existing CSV record?
Answer: A common method is to read the records, modify the desired data, and write the result to a new or temporary file. Validate the result before replacing the original.
Q15. When should you choose a database instead of CSV?
Answer: Use a database when the application needs complex queries, relationships, transaction guarantees, concurrent updates, or robust management of large datasets.
23. Quick Revision Checklist
- Understand the purpose and structure of CSV files.
- Import Python's built-in
csv module.
- Read records using
csv.reader().
- Read records by column name using
csv.DictReader().
- Write sequences with
csv.writer().
- Write dictionaries with
csv.DictWriter().
- Use
writerow(), writerows(), and writeheader() correctly.
- Open CSV files with
newline="" and an appropriate encoding.
- Configure delimiters and quoting for nonstandard CSV formats.
- Convert string values into numeric types when calculations are required.
- Handle empty files, missing fields, malformed records, and file exceptions.
- Validate data before updating important CSV files.
- Consider spreadsheet formula injection when exporting untrusted data.
- Know when CSV is suitable and when a database or another structured format is more appropriate.
24. Conclusion
The Python csv module makes it straightforward to read, write, filter, and process tabular text data. The main tools are csv.reader() and csv.writer() for sequence-based operations, and csv.DictReader() and csv.DictWriter() for working with named columns.
Reliable CSV processing requires correct file modes, explicit encoding, newline="", appropriate data-type conversion, and validation of missing or malformed values. Using the built-in CSV module also prevents many parsing problems that arise from manually splitting strings.
Practice by creating a student record manager, an employee report generator, or an inventory tracker. These exercises provide a practical foundation for data analysis, automation, reporting, and other Python development tasks.
Continue learning: Explore the Python Training Course at JustAcademy, register for a course demo, or visit the JustAcademy website.